package pbl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 
 * @author @author AMPM
 *
 */

public class TcpIMClient {

	  public static void main (String args[]) throws Exception{
			// an object representing this computers IP address
		    InetAddress serverAddress = InetAddress.getByName("localhost");
		    int serverPort = 7896;

		    // we request to establish a socket connection to a remote server
		    Socket socket = new Socket( serverAddress , serverPort ); // Martin IP: "10.25.250.249"	    
		    
		    while (true) {
		    
		    // get the output stream from the socket
		    OutputStream os = socket.getOutputStream();
		    // wrap it in the ObjectOutputStream object
		    ObjectOutputStream oos = new ObjectOutputStream( os );
		    
		    // get an input from user
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String message = br.readLine().trim();		    	
		    	
		    // the data for sending is written to the (wrapped) output stream
		    oos.writeObject( message );
		    
		    // get the input stream from the socket
		    InputStream is = socket.getInputStream();
		    // wrap it in the ObjectOutputStream object
		    ObjectInputStream ois = new ObjectInputStream( is );
		    
		    // get the response from the server and cast it to a String object
		    String response = (String) ois.readObject();
		    
		    System.out.println( response );
		    
		    }
	  }
		}
